<?php

namespace app\index\controller;

use app\common\model\AchievementAnalysisModel;
use app\common\model\UserModel;
use app\index\controller\IndexBase;

/** 
 *  前端用户控制器
 * 
 * 时间：2020年4月11日23:08:14
 * 作者：夏立军
 *
 */
class User extends IndexBase
{
    // 首页
    public function index()
    {
        $uid = session('index_login')->id;
        $user = new UserModel();
        $user_info_data = $user->get_user_info_by_id($uid);

        $permissions_res = $user->get_user_all_permissions_by_uid($uid)->toArray();

        $user_exist_permissions = $user->get_user_exist_permissions_by_uid($uid);

        $all_super_authority = false;
        $common_role_edit = false;
        $common_role_password = false;
        $results_management = false;
        $results_the_audit = false;

        // 如果没有超级权限
        if (!$user_exist_permissions) {
            foreach ($permissions_res as $row) {
                // 个人信息
                if ('index/User/user_edit' == $row['url']) {
                    $common_role_edit = true;
                }
                // 修改密码
                if ('index/User/user_password' == $row['url']) {
                    $common_role_password = true;
                }

                // 成果管理
                if ('index/Achievement/index' == $row['url']) {
                    $results_management = true;
                }

                // 成果审核
                if ('index/AchievementAudit/index' == $row['url']) {
                    $results_the_audit = true;
                }
            }
        } else {
            $all_super_authority = true;
        }

        // 成果分析
        $achievement_analysis = new AchievementAnalysisModel();
        $achievement_analysis_res = $achievement_analysis->get_achievement_analysis_data_by_uid($uid);

        // 统计总量 已通过的数量
        $achievement_analysis_res = json_encode($achievement_analysis_res);

        // 获取所有的成果总量以及通过的数量
        $achievement_analysis_through_count_res = $achievement_analysis->get_achievement_analysis_user_data_by_uid($uid);
        $achievement_analysis_through_count_res = json_encode($achievement_analysis_through_count_res);

        return view('index', [
            // 个人的数据信息
            'user_info_data' => $user_info_data,

            // 功能列表
            // 超级权限
            'all_super_authority' => $all_super_authority,

            // 个人信息 与 修改密码
            'common_role_edit' => $common_role_edit,
            // 密码修改
            'common_role_password' => $common_role_password,
            // 成果管理
            'results_management' => $results_management,
            // 成果审核
            'results_the_audit' => $results_the_audit,
            // 柱状图
            'achievement_analysis_res' => $achievement_analysis_res,
            // 分布图
            'achievement_analysis_through_count_res' => $achievement_analysis_through_count_res,
        ]);
    }

    // 用户信息修改
    public function user_edit()
    {
        if (request()->post()) {

            $id = input('post.id/d', null);
            $name = input('post.name/s', null);
            $age = input('post.age/d', null);
            $position = input('post.position/s', null);
            $tel = input('post.tel/s', null);
            $phone = input('post.phone/s', null);
            $email = input('post.email/s', null);
            $qq = input('post.qq/s', null);
            $wx = input('post.wx/s', null);
            $address = input('post.address/s', null);

            $data['name'] = $name;
            $data['age'] = $age;
            $data['position'] = $position;
            $data['tel'] = $tel;
            $data['phone'] = $phone;
            $data['email'] = $email;
            $data['qq'] = $qq;
            $data['wx'] = $wx;
            $data['address'] = $address;
            $data['update_time'] = time();

            $user = new UserModel();
            $user_res = $user->set_user_info($id, $data);
            if (!$user_res) {
                return json(['code' => 1, 'msg' => '修改失败，【手机/邮箱/QQ/微信】可能与其他人重复']);
            }

            session('index_login', $user->get_user_info_by_id($id));
            return json(['code' => 0, 'msg' => '修改成功']);
        }

        $uid = input('get.uid/d', null);
        if (is_null($uid)) {
            return redirect_index('未传入用户ID', url('User/index'));
        }

        $user = new UserModel();
        $user_res = $user->get_user_info_by_id($uid);

        if (is_null($user_res)) {
            return redirect_index('未传入用户ID', url('User/index'));
        }

        return view('user_edit', [
            'user_info_data' => $user_res,
        ]);
    }

    // 用户密码修改
    public function user_password()
    {

        if (request()->post()) {
            $uid = input('post.uid/d', null);
            $old_password = input('post.old_password/s', null);
            $new_password = input('post.new_password/s', null);
            $confirm_password = input('post.confirm_password/s', null);

            if (is_null($uid)) {
                return json(['code' => 1, 'msg' => 'UID 不存在']);
            }

            if (is_null($old_password) || is_null($new_password) || is_null($confirm_password)) {
                return json(['code' => 2, 'msg' => '密码不能为空']);
            }
            if (mb_strlen($new_password) < 6) {
                return json(['code' => 3, 'msg' => '新密码不能少于6位']);
            }
            if ($new_password != $confirm_password) {
                return json(['code' => 4, 'msg' => '两次密码不一致']);
            }

            $user = new UserModel();
            $user_info = $user->get_user_info_by_id($uid);
            if (!$user_info) {
                return json(['code' => 5, 'msg' => '用户不存在']);
            }

            if ($user_info['password'] != md5($old_password . config('ams_config.user_password_suffix'))) {
                return json(['code' => 6, 'msg' => '旧密码不正确']);
            }

            $password = md5($new_password . config('ams_config.user_password_suffix'));
            $data['password'] = $password;

            $user_res = $user->set_user_info($uid, $data);
            if (!$user_res) {
                return json(['code' => 7, 'msg' => '密码修改失败']);
            }

            return json(['code' => 0, 'msg' => '密码修改成功']);
        }

        $uid = input('get.uid/d', null);
        if (is_null($uid)) {
            return redirect_index('未传入用户ID', url('User/index'));
        }

        return view('user_password', [
            'user_id' => $uid
        ]);
    }
}
